
package com.jfzt.controller;

import com.jfzt.common.ResultMapping;
import com.jfzt.config.Loggable;
import com.jfzt.pojo.*;
import com.jfzt.service.IElasticService;
import com.jfzt.utils.ElasticsearchServiceImplUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author: shunpeng.hu
 * @date: 2024/1/8 14:03
 */

@RestController
public class TestController {

    @Resource
    private IElasticService elasticService;

    @Resource
    private RestHighLevelClient client;


    @PostMapping("/login")
    @Loggable(type = "SELECT", description = "域账号登录")
    public KeycloakToken loginLog(@RequestBody KCRealmClient kcRealmClient, HttpServletRequest request) {
        KCRealmClient kcRealmClient1 = new KCRealmClient();
        RestTemplate restTemplate = new RestTemplate();
        UserPayload userPayload = new UserPayload();
        kcRealmClient1.setKcRealm("Test");
        kcRealmClient1.setKcAdmin("admin.test");
        kcRealmClient1.setKcPassword("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI0ZWRlMGU0ZmVjMDk0MjM1YTJmMjk5MTgwNThkMDA0OSIsInN1YiI6ImpmenQudGVzdCIsImlzcyI6ImFkbWluIiwiaWF0IjoxNjk3MDA4OTE3LCJleHAiOjE3MTI1NjA5MTd9.vLr_K_QSAXonSTXkmjkeLzQVwjew-HG3tZiPhDALV3o");
        kcRealmClient1.setKcClient("后端");
        userPayload.setUsername("shunpeng.hu");
        userPayload.setPassword("Husp295914");

        Map<String, Object> map = new HashMap<>(10);
//        map.put("kcAdmin", kcRealmClient.getKcAdmin());
//        map.put("kcRealm", kcRealmClient.getKcRealm());
//        map.put("kcClient", kcRealmClient.getKcClient());
        map.put("kcRealmClient", kcRealmClient);
        map.put("userPayload", userPayload);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        request.setAttribute("kcUri", "http://172.16.37.1:31201/v2/api/login");
        Date start = new Date();
        request.setAttribute("requestDate", simpleDateFormat.format(start));

        ResponseEntity<KeycloakToken> response = restTemplate.postForEntity("http://172.16.37.1:31201/v2/api/login", map, KeycloakToken.class);
        KeycloakToken body = response.getBody();
        request.setAttribute("data", body);
        request.setAttribute("responseBody", response);
        // 单独set
        request.setAttribute("业务说明", "用户AD域账户登录");
        // 单独set
        request.setAttribute("业务所属类型", "");

        Date end = new Date();
        request.setAttribute("responseDate", simpleDateFormat.format(end));

        request.setAttribute("耗时", (end.getTime() - start.getTime()) + "ms");
        return body;
    }

    /**
     * 集群查询接口
     * @param queryParamDto queryParamDto
     * @return document
     */
    @PostMapping("/queryMatchAll")
    public ResultMapping queryMatchAll(@RequestBody QueryParamDto queryParamDto) {
        return elasticService.queryMatchAll(queryParamDto);
    }

    /**
     * GET /indexName/_search
     * {
     * "query": {
     * "match": {
     * "字段类型": "字段值"
     * }
     * },
     * "from": 起始索引,
     * "size": 每页显示条数
     * }
     */
    @PostMapping("/queryMatchByFeild")
    public ResultMapping queryMatchByFeild(@RequestBody QueryParamDto queryParamDto) {
        return elasticService.queryMatchFeild(queryParamDto);
    }

    /**
     * GET /keycloak_util_dev/_search
     * {
     * "query": {
     * "multi_match": {
     * "query": "intellectual_property_management",
     * "fields": ["kcAdmin", "kcRealm"]
     * }
     * },
     * "from": 0,
     * "size": 100
     * }
     */
    @PostMapping("queryMulitMatchByFeild")
    @Loggable
    public ResultMapping queryMulitMatchByFeild(@RequestBody QueryParamDto queryParamDto) {
        return elasticService.queryMulitMatchByFeild(queryParamDto);
    }

    @GetMapping("/queryIndex")

    public String queryIndex(@RequestParam String indexName) {
        String row = elasticService.queryLoginIndex(indexName);
        return "查询索引库成功";
    }

    @PostMapping("/createIndex")
    public String createIndex(@RequestParam String indexName) {

        elasticService.createLoginIndex(indexName);
        return "创建登录索引库成功";
    }

    @PostMapping("/createDocument")
    public String createDocument(@RequestParam String indexName, @RequestBody Map<String, Object> document) {
        elasticService.createLoginDocument(indexName, document);
        return "创建登录索引文档成功！";
    }

    @DeleteMapping("/deleteLoginIndex")
    public String deleteLoginIndex(@RequestParam String indexName) {
        elasticService.deleteLoginIndex(indexName);
        return "删除登录索引库成功";
    }

    @GetMapping("/isExistsIndex")
    public String isExistsIndex(@RequestParam String indexName) {
        boolean flag = ElasticsearchServiceImplUtils.checkIndexExists(indexName);
        return "索引已存在" + flag;
    }



}

